<template>
    <headers title='连接蓝牙' back-icon-name="nav-back" background="#3cbafd" />

    <view class="bluetooth-container">
        <u-button type="primary" @tap="startSearch">开始扫描</u-button>
        <view class="bluetooth-list">
            <u-cell-group v-if="blueToothList.length">
                <u-cell-item v-for="item in blueToothList" :key="item.deviceId" :title="item.name" :label="item.deviceId"
                    :value="'强度：' + item.RSSI + 'dBm'" :arrow="false" :index="item.deviceId" @click="clickCell">
                    <template #icon>
                        // #ifdef APP-PLUS
                        <u-icon name="lanya1" custom-prefix="custom-icon" size="20" color="#888888"
                            style="margin-right: 20rpx;" />
                        // #endif
                        // #ifdef H5
                        <u-icon name="lanya1" custom-prefix="custom-icon" size="32" color="#888888"
                            style="margin-right: 20rpx;" />
                        // #endif
                    </template>
                </u-cell-item>
            </u-cell-group>
            <u-empty v-else text="数据为空" mode="list" style="margin-top: 50rpx;" />
        </view>
    </view>
</template>

<script setup lang='ts'>
import { ref, computed } from 'vue'
import { useBlueTooths } from '@/tspl/blueTooth';
import headers from '@/components/header/index.vue'

const { blueToothList, startSearch, chooseBluetooth, stopBluetoothDeviceDiscovery } = useBlueTooths()

// rssi范围
// const rssiRange = computed(() => {
//     let rssiArr = [] as number[]
//     demo.map((item: any) => {
//         rssiArr.push(item.RSSI)
//     })
//     let maxNumber = Math.max.apply(null, rssiArr);
//     let minNumber = Math.min.apply(null, rssiArr);
//     console.log(rssiArr, maxNumber, minNumber);

// })

const clickCell = (deviceId: string) => {
    chooseBluetooth(deviceId)
}
</script>
 
<style lang="scss" scoped>
.bluetooth-container {
    padding: 20rpx;

    .bluetooth-list {
        margin-top: 20rpx;
    }
}
</style>